package com.jy.adscm.hcPlan.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.hcPlan.bean.*;
import com.jy.adscm.hcPlan.mapper.mysql.*;
import com.jy.adscm.hcPlan.service.HcPlanMakePartExampleService;
import com.jy.adscm.hcPlan.service.HcPlanSaleService;
import com.jy.adscm.utils.AdscmBaseUtils;
import com.jy.adscm.utils.AdscmDateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * <p>
 * 销售计划表 服务实现类
 * </p>
 *
 * @author yyy
 * @since 2018-08-13
 */
@Service("hcPlanSaleService")
public class HcPlanSaleServiceImpl extends ServiceImpl<HcPlanSaleMapper, HcPlanSale> implements HcPlanSaleService {

    Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    HcPlanSaleMapper hcPlanSaleMapper;
    @Autowired
    HcPlanProcedureMapper hcPlanProcedureMapper;
    @Autowired
    HcPlanFinalAssemblyMapper hcPlanFinalAssemblyMapper;
    @Autowired
    HcPlanAfterFinalAssemblyMapper hcPlanAfterFinalAssemblyMapper;
    @Autowired
    HcPlanAfterFinalAssemblyExampleMapper hcPlanAfterFinalAssemblyExampleMapper;
    @Autowired
    HcPlanMakePartExampleService hcPlanMakePartExampleService;

    @Override
    public List<HcPlanSale> getAllSalePlan() throws AdscmSystemException {

        logger.info("查询所有销售计划...");
        List<HcPlanSale> planSaleList = null;
        try{
            planSaleList = hcPlanSaleMapper.getAllSalePlan(null);
        }catch (Exception e){
            logger.error("查询所有销售计划异常",e);
            throw new AdscmSystemException(e.getMessage());
        }
        return planSaleList;
    }

    @Transactional
    @Override
    public void addSalePlan(HcPlanSale hcPlanSale) throws AdscmSystemException {
/*

        logger.info("增加销售计划...");
//        //保存销售计划
//        hcPlanSaleMapper.insert(hcPlanSale);

        //获取总装所有工序的标准时间配置
        Integer ppCycleDay = hcPlanProcedureMapper.getAllCycleDay();
        if (ppCycleDay == null){
            ppCycleDay = 0;
        }
        logger.info("总装所有工序耗费的标准时间：【"+ppCycleDay+"天】");

        //看这个销售计划有多少批次
        List<HcPlanSaleGoods> planSaleGoods = hcPlanSale.getPlanSaleGoods();

//        Integer aNum = hcPlanSale.getArrivalNum();

        if (planSaleGoods != null && planSaleGoods.size() > 0){
            logger.info("销售计划的批次数量：【"+planSaleGoods.size()+"批】");
            for (HcPlanSaleGoods goods :
                    planSaleGoods) {
                //生成总装数据
                HcPlanFinalAssembly hpfa = new HcPlanFinalAssembly();
                hpfa.setSale_id(hcPlanSale.gettId());
                hpfa.setAss_cycle_day(ppCycleDay);
                hpfa.setGoods_id(goods.gettId());//商品id

                Date arrivalDate = goods.getPlanArrivedTime();//hcPlanSale.getArrivalDate();//到货日期

                Date[] allDate = this.getAssAllDate(arrivalDate);
                hpfa.setAss_start_date(allDate[0]);
                hpfa.setAss_end_date(allDate[1]);


                //保存总装数据
                hcPlanFinalAssemblyMapper.insert(hpfa);
                logger.info("插入总装数据：【"+hpfa+"】");

                hcPlanMakePartExampleService.loadHcPartExampleByAssemblyId(hpfa.getId());

                //生成单次销售计划耗材总装后的其他计划项目标准配置
                List<HcPlanAfterFinalAssembly> hcPlanAfterAssembly = hcPlanAfterFinalAssemblyMapper.selectList(null);
                if (hcPlanAfterAssembly != null && hcPlanAfterAssembly.size()>0){
                    for (HcPlanAfterFinalAssembly hpab : hcPlanAfterAssembly){
                        HcPlanAfterFinalAssemblyExample hpfae = new HcPlanAfterFinalAssemblyExample();
                        hpfae.setFinalAssembly(hpfa.getId());
                        hpfae.setCycleDay(hpab.getCycleDay());
                        hpfae.setProjectName(hpab.getProjectName());
                        HcPlanAfterFinalAssemblyExample hpfae_temp =  hcPlanAfterFinalAssemblyExampleMapper.selectOne(hpfae);
                        if(hpfae_temp != null && hpfae_temp.getId() != null){
                            //更新
                            hpfae.setId(hpfae_temp.getId());
                            hcPlanAfterFinalAssemblyExampleMapper.updateAllColumnById(hpfae);
                        }else {
                            hcPlanAfterFinalAssemblyExampleMapper.insert(hpfae);
                        }
                    }

                }

            }



        }

*/

    }

    @Transactional
    @Override
    public void delSalePlanGoods(HcPlanSaleGoods goods) throws AdscmSystemException {
//        logger.info("删除销售计划【"+goods.gettId()+"】中的商品【"+goods.getGoodsName()+"】");
//
//        try{
//
//            if(!AdscmBaseUtils.getInstance().isNotEmpty(goods.getSpId(),null)){
//                logger.info("删除销售计划【"+goods.gettId()+"】中的商品【"+goods.getGoodsName()+"】失败");
//                throw new AdscmSystemException("没有商品编号");
//            }
//            if(!AdscmBaseUtils.getInstance().isNotEmpty(goods.gettId(),null)){
//                logger.info("删除销售计划【"+goods.gettId()+"】中的商品【"+goods.getGoodsName()+"】失败");
//                throw new AdscmSystemException("没有销售ID");
//            }
//
//            Map<String,Object> map = new HashMap<>();
//            map.put("goods_id",goods.getSpId());//商品编号
//            map.put("sale_id",goods.gettId());//销售任务ID
//            List<HcPlanFinalAssembly> hpfa = hcPlanFinalAssemblyMapper.selectByMap(map);
//
//            if (hpfa != null && hpfa.size() > 0){
//                //把它删掉
//                HcPlanFinalAssembly hpfab = hpfa.get(0);
//                //
//                Map<String,Object> delMap = new HashMap<>();
//                delMap.put("final_assembly",hpfab.getId());//总装编号
//                hcPlanAfterFinalAssemblyExampleMapper.deleteByMap(delMap);
//                hcPlanFinalAssemblyMapper.deleteById(hpfab.getId());
//            }
//
//        }catch (Exception e){
//            logger.error("删除销售计划【"+goods.gettId()+"】中的商品【"+goods.getGoodsName()+"】异常",e);
//            throw new AdscmSystemException(e.getMessage());
//        }

    }

    @Transactional
    @Override
    public void editSalePlanGoods(HcPlanSaleGoods goods) throws AdscmSystemException {
//        logger.info("更新销售计划【"+goods.gettId()+"】中的商品【"+goods.getGoodsName()+"】");
//        try{
//            if(!AdscmBaseUtils.getInstance().isNotEmpty(goods.getSpId(),null)){
//                logger.info("更新销售计划【"+goods.gettId()+"】中的商品【"+goods.getGoodsName()+"】失败");
//                throw new AdscmSystemException("没有商品编号");
//            }
//            if(!AdscmBaseUtils.getInstance().isNotEmpty(goods.gettId(),null)){
//                logger.info("更新销售计划【"+goods.gettId()+"】中的商品【"+goods.getGoodsName()+"】失败");
//                throw new AdscmSystemException("没有销售ID");
//            }
//
//            Map<String,Object> map = new HashMap<>();
//            map.put("goods_id",goods.getSpId());//商品编号
//            map.put("sale_id",goods.gettId());//销售任务ID
//            List<HcPlanFinalAssembly> hpfa = hcPlanFinalAssemblyMapper.selectByMap(map);
//
//            if (hpfa != null && hpfa.size() > 0){
//                //修改它
//                HcPlanFinalAssembly hpfab = hpfa.get(0);
//                Date arrivalDate = goods.getPlanArrivedTime();//hcPlanSale.getArrivalDate();//到货日期
//
//                Date[] allDate = this.getAssAllDate(arrivalDate);
//                hpfab.setAss_start_date(allDate[0]);
//                hpfab.setAss_end_date(allDate[1]);
//
//                hcPlanFinalAssemblyMapper.updateById(hpfab);
//            }
//        }catch (Exception e){
//            logger.error("更新销售计划【"+goods.gettId()+"】中的商品【"+goods.getGoodsName()+"】异常",e);
//            throw new AdscmSystemException(e.getMessage());
//        }
    }



}
